import ScrollBarLayout from '@/components/ScrollBarLayout';
import { DescriptionsType } from '@/components/DescriptionsType';
import { getOnlyEditingTempConfig, ntkoQueryParams } from '../mconfig';
import { NtkoPlugin } from '@/common/NtkoPluginType';
import { saveToURLWordService } from '../services';
import { useContext, useMemo, useState } from 'react';
import { ContractManageAPI } from '../common/type';
import { wordHandChildParam, wordToChildParams } from '@/utils';
import { Spin } from 'antd';
import { ConractControllerContext } from '../common';
import { getBaseUrl } from '@/services/urlconst';

export const ContractOnlyEditingView: React.FC<ContractManageAPI.IContractOnlyEditingView> = ({ contractFormInfo }) => {
  /** 引入上下文 */
  const { curRes } = useContext(ConractControllerContext);

  /** 保存在线编辑权限 */
  const [saveNtkoQuery, setSaveNtkoQuery] = useState<Record<string, any>>({
    isReadOnly: 0,
    filedUrl: '',
    ntkoWordUrl: '',
  });

  /** 保存文件接口回调 */
  const saveToUrlWordCallback = (res: any) => {
    const messageObj = {
      msgFlag: res?.msgFlag,
      msgText: res?.msg,
      msgType: 'uploadFiledRequest',
    };
    wordToChildParams(ntkoBrowser, saveNtkoQuery?.ntkoWordUrl, messageObj);
  };

  /** 保存文件接口 */
  const { wordSaveToURLRun, wordSaveToURLLoading } = saveToURLWordService(saveToUrlWordCallback);

  /** 保存文件回调 */
  const ntkoCallback = (childValue) => {
    const childBase = JSON.parse(childValue);
    if (childBase?.baseUrl && childBase?.baseObj) {
      const fielUrl = childBase?.baseUrl?.split(getBaseUrl())?.[1];
      wordSaveToURLRun({
        ...childBase,
        baseUrl: fielUrl,
      });
    }
  };

  /** 引入在线预览 */
  const ntkoBrowser = useMemo(() => {
    const { ntkoBrowser } = NtkoPlugin(ntkoCallback);
    return ntkoBrowser;
  }, []);

  /** 处理相对方信息 */
  const oppositeBookMark = (oppoList: Record<string, any>[]) => {
    let supplyNames: string = '';
    let supplyBankConfig: string = '';
    oppoList?.forEach((item: any, index: number) => {
      supplyNames += oppoList?.length - 1 == index ? item?.oppositeName : `${item?.oppositeName}，`;
      supplyBankConfig += oppoList?.length - 1 == index ? `${item?.bankName}，${item?.bankAccount}` : `${item?.bankName}，${item?.bankAccount}。`;
    });
    return {
      supplyNames,
      supplyBankConfig,
    };
  };

  /** 点击在线预览文件 */
  const onNtkoReadWord = (urlName: string, urlObj: Record<string, any>, onlyRead?: boolean) => {
    const { filedUrl, ntkoWordUrl } = ntkoQueryParams(urlObj, curRes, urlName);
    setSaveNtkoQuery({
      filedUrl,
      ntkoWordUrl,
    });

    const oppoConfig: Record<string, any> = oppositeBookMark(urlObj?.oppositeList);
    const formInfo: Record<string, any> = {
      ...urlObj,
      ...oppoConfig,
    };

    const childStr = wordHandChildParam(formInfo);
    const extensBool = ntkoBrowser.ExtensionInstalled();
    if (extensBool) {
      ntkoBrowser?.openWindow(ntkoWordUrl, false, '', childStr);
    }
  };

  /** 获取表单配置 */
  const { config } = getOnlyEditingTempConfig(onNtkoReadWord);

  return (
    <Spin spinning={wordSaveToURLLoading}>
      <div className="commonTabsBodyDom">
        <div className={'tabsBodyInfoDom'}>
          <ScrollBarLayout style={{ padding: '5px 20px' }}>
            <DescriptionsType config={config} />
          </ScrollBarLayout>
        </div>
        {/* <div className="tabsBodyBottomBtn">
          <Button type={'primary'} icon={<DiffOutlined />}>
            返回
          </Button>
        </div> */}
        {/* <UpdateContractBottom
          nextBtnShow={false}
          isBudgetNotBtn={false}
          setTabsInfoPaneInfo={() => { }}
          addContractRun={addContractRun}
          editContractRun={editContractRun}
          requestType={false}
          tabPaneContactNtko={true}
        /> */}
      </div>
    </Spin>
  );
};
